package UI;

import Auth.Authentication;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by IntelliJ IDEA.
 * User: mchebanenko
 * Date: 11-Aug-2010
 * Time: 4:43:16 PM
 * To change this template use File | Settings | File Templates.
 */


public class User {

    public static final String INVALID_LOGIN_OUTCOME = "invalid";
    public static final String VALID_LOGIN_OUTCOME = "valid";

    private String userName = null;
    private String password = null;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String validateUser (){


       try{
            Context cont = new InitialContext();
           // JBOSS
           int res = ((Authentication) cont.lookup("HotelManager/AuthenticationBean/local")).validateUser(userName,password);
           
           // int res = ((Authentication) cont.lookup("java:global/HotelManager/EJB/AuthenticationBean")).validateUser(userName,password);
           
            if (res == 1)
                return  VALID_LOGIN_OUTCOME;
            else
                return  INVALID_LOGIN_OUTCOME;
        } catch (Exception e) {
            e.printStackTrace();  
            return  INVALID_LOGIN_OUTCOME;
        }
    }


    public void sendToWeb2 (){
        try{
            FacesContext cont =  FacesContext.getCurrentInstance();
            HttpServletResponse resp = (HttpServletResponse)cont.getExternalContext().getResponse();
            resp.sendRedirect("/WEB2/content/Hello.html");
            cont.responseComplete();

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
